package com;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowConfig;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

public class MainConfig extends JFinalConfig {

    private static UndertowServer undertowServer;

    private static DruidPlugin druidPlugin;

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
        PropKit.use("appconfig.properties");
    }

    @Override
    public void configRoute(Routes me) {
        me.scan("com.");
    }

    @Override
    public void configEngine(Engine me) {
        me.setBaseTemplatePath("webapp");
        me.setToClassPathSourceFactory();
    }

    @Override
    public void configPlugin(Plugins me) {
        druidPlugin = new DruidPlugin(PropKit.get("jdbc.Url"), PropKit.get("jdbc.userName"),
                PropKit.get("jdbc.password"), PropKit.get("jdbc.driver"));
        me.add(druidPlugin);
        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
        me.add(activeRecordPlugin);
    }

    @Override
    public void configInterceptor(Interceptors me) {
    }

    @Override
    public void configHandler(Handlers me) {
    }

    @Override
    public void onStart() {
        UndertowConfig undertowConfig = undertowServer.getUndertowConfig();
        System.err.println("=================getIoThreads=======>" + undertowConfig.getIoThreads());
        System.err.println("=================getWorkerThreads===>" + undertowConfig.getWorkerThreads());
        System.err.println("=================getBufferSize======>" + undertowConfig.getBufferSize());
        System.err.println("=================getJFinalConfig====>" + undertowConfig.getJFinalConfig());
        System.err.println("=================getMaxActive=======>" + druidPlugin.getDruidDataSource().getMaxActive());
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.err.println(" successful ^_^ ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void onStop() {
        int number = 0;
        while (number < 5) {
            try {
                Thread.sleep(1000);
                System.err.println("=====执行停止命令====");
                number++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        undertowServer = UndertowServer.create(MainConfig.class, "undertow.properties");
        undertowServer.start();
    }

}
